package com.thxr.sso.server.controller;

import com.thxr.sso.client.rpc.Result;
import com.thxr.sso.server.pojo.Entry;
import com.thxr.sso.server.service.EntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Author: silence.Z
 * @CreateTime: 2020/11/9 16:18
 * @Description:
 */
@Controller
@RequestMapping("zentao")
public class ZentaoController {
    //"项目管理系统--后台--二次开发--应用"中的代号
    private static final String code = "ssoxm";
    @Autowired
    private EntryService entryService;

    @GetMapping("login")
    public String login(@RequestParam("userName")String userName) {
        //http://xm.jxthxr.com/api.php?m=user&f=apilogin&account=silenceZheng&code=test1&time=1604911535858&token=1896AF0EB1914997CED35154E10F417D
        String account = userName;
        String timestamp = String.valueOf(System.currentTimeMillis());

        //根据代号获得密钥
        Result result = entryService.findByCode(code);
        Entry entry = (Entry) result.getData();
        String key = entry.getKey();

        String token = DigestUtils.md5DigestAsHex((code+key+timestamp).getBytes());
        return "redirect:http://xm.jxthxr.com/api.php?m=user&f=apilogin&account="+userName+"&code="+code+"&time="+timestamp+"&token="+token;
    }
}
